//
//  MR_Convert.m
//  MeiRiMoney
//
//  Created by qiuqk on 13-6-25.
//  Copyright (c) 2013年 MeiRiCai. All rights reserved.
//

#import "MR_Convert.h"

@implementation MR_Convert

+ (NSString *) getStringWithKey:(NSString *)key fromDictionary:(NSDictionary *)dictionary
{
	return [self getValue:key fromDictionary:dictionary];
}

+ (long) getIntWithKey:(NSString *)key fromDictionary:(NSDictionary *)dictionary
{
	NSNumber *value = [self getValue:key fromDictionary:dictionary];
	return value.intValue;
}

+ (long) getLongWithKey:(NSString *)key fromDictionary:(NSDictionary *)dictionary
{
	 NSNumber *value = [self getValue:key fromDictionary:dictionary];
	 return value.longValue;
}

+ (double) getDoubleWithKey:(NSString *)key fromDictionary:(NSDictionary *)dictionary
{
	NSNumber *value = [self getValue:key fromDictionary:dictionary];
	return value.doubleValue;
}

+ (NSDecimalNumber *) getDecimalWithKey:(NSString *)key fromDictionary:(NSDictionary *)dictionary
{
	NSNumber * value = [self getValue:key fromDictionary:dictionary];
	return [NSDecimalNumber decimalNumberWithDecimal:value.decimalValue];
}

+ (NSDate *) toDateFromJson:(NSString *)key fromDictionary:(NSDictionary *)dictionary
{
	NSString *value = [self getValue:key fromDictionary:dictionary];
	
	NSString *pattern = @"Date\\((\\d+)\\+(\\d+)\\)";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    NSTextCheckingResult *result = [regex firstMatchInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];
    if (nil == result)
    {
        return nil;
    }
    NSRange range = [result rangeAtIndex:1];
    double seconds = [[value substringWithRange:range] longLongValue] / 1000.0;
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:seconds];
    
    return date;
}

+ (id) getValue:(NSString *)key fromDictionary:(NSDictionary *)dictionary
{
	return [dictionary valueForKey:key];
}

+ (NSString *) intToString:(int)value
{
	return [NSString stringWithFormat:@"%i", value];
}

+ (NSString *) longToString:(long)value
{
	return [NSString stringWithFormat:@"%li", value];
}

+ (NSString *) floatToString:(float)value
{
	return [NSString stringWithFormat:@"%f", value];
}

+ (NSString *) doubleToString:(double)value
{
	return [NSString stringWithFormat:@"%f", value];
}

@end
